﻿using System;
using System.Collections.Generic;
using BCCommon;
using BCCommon.Enums;
using BCDto.Common.NotificationMessageRecord;
using BCService.Utils;
using BCData.TMS.NotificationMessageRecord;
using BCDto.Sim.Admin.Admin;
using BCDto.Website.User;
using BCEntity.TMS.NotificationMessageRecord;

namespace BCService.Common.NotificationMessageRecord
{
    public class NotificationMessageRecordService : INotificationMessageRecordService
    {
        private readonly INotificationMessageRecordData notificationMessageRecordData;

        public NotificationMessageRecordService(
            INotificationMessageRecordData notificationMessageRecordData)
        {
            this.notificationMessageRecordData = notificationMessageRecordData;
        }

        public NotificationMessageRecordDto GetNotificationMessageRecord(long notificationMessageId)
        {
            var entity = this.notificationMessageRecordData.GetNotificationMessageRecordAsync(notificationMessageId).Result;

            return entity.As<NotificationMessageRecordDto>();
        }

        public PageableList<NotificationMessageRecordDto> GetPageableList(AdminDto admin, UserDto user, bool? isRead, NotificationMessageRecordType? notificationMessageRecordType, int pageIndex, int pageSize)
        {
            if (admin != null)
            {
                var (list, count) = this.notificationMessageRecordData.GetAdminMessagesAsync(admin.AdminId, isRead, notificationMessageRecordType, pageIndex, pageSize).GetAwaiter().GetResult();

                return new PageableList<NotificationMessageRecordDto>()
                {
                    Items = list.TransformTo<NotificationMessageRecordDto>(),
                    Count = count
                };
            }

            if (user != null)
            {
                var (list, count) = this.notificationMessageRecordData.GetUserMessagesAsync(user.UserId, isRead, notificationMessageRecordType, pageIndex, pageSize).GetAwaiter().GetResult();

                return new PageableList<NotificationMessageRecordDto>()
                {
                    Items = list.TransformTo<NotificationMessageRecordDto>(),
                    Count = count
                };
            }

            return new PageableList<NotificationMessageRecordDto>
            {
                Items = new List<NotificationMessageRecordDto>(),
                Count = 0
            };
        }

        public int GetUnreadCount(AdminDto admin, UserDto user)
        {
            if (admin != null)
            {
                return this.notificationMessageRecordData.GetAdminUnreadCountAsync(admin.AdminId).Result;
            }

            if (user != null)
            {
                return this.notificationMessageRecordData.GetAdminUnreadCountAsync(user.UserId).Result;
            }

            return 0;
        }

        public IEnumerable<NotificationGroupMessageCountDto> GetGroupUnreadCount(AdminDto admin, UserDto user)
        {
            if (admin != null)
            {
                return notificationMessageRecordData.GetAdminGroupUnreadCountAsync(admin.AdminId).Result.TransformTo<NotificationGroupMessageCountDto>();
            }

            if (user != null)
            {
                return notificationMessageRecordData.GetUserGroupUnreadCountAsync(user.UserId).Result.TransformTo<NotificationGroupMessageCountDto>();
            }

            return new List<NotificationGroupMessageCountDto>();
        }

        public NotificationMessageRecordDto Create(NotificationMessageRecordRequestDto requestDto)
        {
            var entity = requestDto.As<NotificationMessageRecordEntity>();
            return this.notificationMessageRecordData.InsertAsync(entity).Result.As<NotificationMessageRecordDto>();
        }





        public NotificationMessageRecordDto UpdateReadStatus(long notificationMessageId)
        {
            return this.notificationMessageRecordData.UpdateReadStatus(notificationMessageId).GetAwaiter().GetResult().As<NotificationMessageRecordDto>();
        }

        public bool UpdateBatchIsRead(AdminDto adminDto, NotificationMessageRecordType? notificationMessageRecordType)
        {
            return notificationMessageRecordData.UpdateBatchIsRead(adminDto.AdminId, notificationMessageRecordType).GetAwaiter().GetResult();
        }

        public NotificationMessageRecordDto UpdateIsHandled(AdminDto adminDto, long notificationMessageId, string handledRemark)
        {
            if (string.IsNullOrWhiteSpace(handledRemark))
            {
                throw new ArgumentException("请填写处理备注");
            }

            var entity = this.notificationMessageRecordData.GetNotificationMessageRecordAsync(notificationMessageId).Result;

            if (entity == null)
            {
                throw new ArgumentException("此通知消息不存在");
            }

            if (entity.AdminId != adminDto.AdminId)
            {
                throw new ArgumentException("您无权修改他人通知信息");
            }

            return this.notificationMessageRecordData.UpdateIsHandled(notificationMessageId, handledRemark).Result.As<NotificationMessageRecordDto>();
        }
    }
}
